//
//  AttackAction.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/9.
//
//

#include "AttackAction.h"
USING_NS_CC;

#define ACTION_TIME (0.09f)

AttackAction::AttackAction()
{
}

AttackAction* AttackAction::create(int type)
{
    AttackAction *pRet = new(std::nothrow) AttackAction();
    if (pRet && pRet->init(type)){
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool AttackAction::init(int type)
{
    if (!Layer::init()) {
        return false;
    }
    animationInit(type);
    
    return true;
}

void AttackAction::animationInit(int type)
{
    auto cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile(getActionPlistName(type));
    
    auto sprite3 = Sprite::createWithSpriteFrameName(getActionFileName(type,1));
    sprite3->setAnchorPoint(Vec2::ZERO);
    sprite3->setPosition(Vec2::ZERO);
    this->addChild(sprite3);
    
    Vector<cocos2d::SpriteFrame *> arr;
    for (int i=1; i<getActionCount(type)+1; i++)
    {
        auto frame_2 = cache->SpriteFrameCache::getInstance()->getSpriteFrameByName(getActionFileName(type,i));
        arr.pushBack(frame_2);
    }
    
    auto animation3 = Animation::createWithSpriteFrames(arr, ACTION_TIME);
    auto action = Animate::create(animation3);
    sprite3->runAction(Sequence::create(action,
                                        CallFuncN::create( CC_CALLBACK_1(AttackAction::callback, this)),
                                        NULL));
}

void AttackAction::callback(Ref* target)
{
    this->removeFromParent();
}

const std::string AttackAction::getActionFileName(int type,int index)
{
    char str[100] = {0};
    switch (type) {
        case SwordAttack:
            sprintf(str, "Cut0%d.png", index);
            break;
        case ArrowAttack:
            sprintf(str, "Arrow0%d.png", index);
            break;
        case FireAttack:
            sprintf(str, "Fire0%d.png", index);
            break;
        case LightAttack:
            sprintf(str, "Light0%d.png", index);
            break;
        case HealthAttack:
            sprintf(str, "Health0%d.png", index);
            break;
        case BombAttack:
        	sprintf(str, "BombAction0%d.png", index);
            break;

    }
    return str;

}

const std::string AttackAction::getActionPlistName(int type)
{
    switch (type) {
        case SwordAttack:
            return "CutAction.plist";
        case ArrowAttack:
            return "ArrowAction.plist";
        case FireAttack:
            return "FireAction.plist";
        case LightAttack:
            return "LightAction.plist";
        case HealthAttack:
            return "HealthAction.plist";
        case BombAttack:
        	return "BombActions.plist";

    }
    return "";
}

int AttackAction::getActionCount(int type)
{
    switch (type) {
        default:
            return 5;
    }
}




